package com.ncell.gui;

import com.ncell.cell.*;
import com.ncell.utils.Utils;
import java.awt.*;
import javax.swing.JPanel;

public class PIUnitCanvas extends JPanel {

    private final int cellWidth = 20;
    private final int cellWidth_div = cellWidth / 2;
    private PIUnit unit;
    private final Color activePColor = Color.DARK_GRAY;
    private final Color inactivePColor = new Color(200, 200, 200);
    private final Color activeIColor = new Color(0, 255, 0);
    private final Color inactiveIColor = new Color(0, 100, 0);
    private final Color inactiveTPColor = new Color(234, 219, 219);
    private final Color activeTPColor = new Color(200, 120, 120);
    private int[] ioffsets;
    private Color[] iColor;

    public PIUnitCanvas() {
        setPreferredSize(new Dimension(500, 500));
    }

    public void setPIUnit(PIUnit unit) {
        this.unit = unit;
        float maxX = 0;
        float maxY = 0;
        for (P p : unit.getPs()) {
            if (p.getX() > maxX) {
                maxX = p.getX();
            }
            if (p.getY() > maxY) {
                maxY = p.getY();
            }
        }

        setPreferredSize(new Dimension((int) (maxX * cellWidth) + cellWidth, (int) (maxY * cellWidth) + cellWidth));
    }

    public PIUnit getUnit() {
        return unit;
    }

    private void initIOffsets() {
        ioffsets = new int[unit.getIs().size()];
        iColor = new Color[unit.getIs().size()];
        for (int i = 0; i < unit.getIs().size(); i++) {
            ioffsets[i] = i * 2;
            iColor[i] = new Color(Utils.getRandomInt(255), Utils.getRandomInt(255), Utils.getRandomInt(255));
        }
    }

    public P getPByXy(int x, int y) {
        for (P p : unit.getPs()) {
            int px = (int) (p.getX() * cellWidth);
            int py = (int) (p.getY() * cellWidth);
            if (px < x && py < y && (px + cellWidth) > x && (py + cellWidth) > y) {
                return p;
            }
        }
        return null;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (ioffsets == null) {
            initIOffsets();
        }

        Graphics2D g2d = (Graphics2D) g;
        g.translate(cellWidth_div, cellWidth_div);
        setFocusable(true);
        for (P p : unit.getPs()) {
            if (p.getActivation() > 0.5) {
                g.setColor(activePColor);
            } else {
                g.setColor(inactivePColor);
            }

            g.fillRect((int) (p.getX() * cellWidth) - cellWidth_div, (int) (p.getY() * cellWidth) - cellWidth_div, cellWidth, cellWidth);
            g.setColor(Color.LIGHT_GRAY);
            g.drawRect((int) (p.getX() * cellWidth) - cellWidth_div, (int) (p.getY() * cellWidth) - cellWidth_div, cellWidth, cellWidth);
        }
        int index = -1;
        for (I i : unit.getIs()) {
            index++;
            if (i.isActiveByOarkn()) {
                g.setColor(activeIColor);
            } else {
                g.setColor(inactiveIColor);
            }

            g.fillOval((int) (i.getX() * cellWidth) - cellWidth_div, (int) (i.getY() * cellWidth) - cellWidth_div, cellWidth, cellWidth);

        }
        for (I tp : unit.getTPlist()) {
            if (tp.isActive()) {
                g.setColor(activeTPColor);
            } else {
                g.setColor(inactiveTPColor);
            }
            g.fillRoundRect((int) (tp.getX() * cellWidth) - cellWidth_div, (int) (tp.getY() * cellWidth) - cellWidth_div, cellWidth, cellWidth, 2, 2);
        }
    }
}
